package com.hnyfkj.jyindustry.biz.chongzhilog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.hnyfkj.jyindustry.biz.chongzhilog.dao.ChongzhiLogDao;
import com.hnyfkj.jyindustry.biz.chongzhilog.dto.ChongzhiLogInsertDto;
import com.hnyfkj.jyindustry.biz.chongzhilog.dto.ChongzhiLogQueryDto;
import com.hnyfkj.jyindustry.biz.chongzhilog.dto.ChongzhiLogUpdateDto;
import com.hnyfkj.jyindustry.biz.chongzhilog.entity.ChongzhiLog;
import com.hnyfkj.jyindustry.biz.chongzhilog.service.ChongzhiLogService;
import com.hnyfkj.jyindustry.biz.item.dao.BarItemDao;
import com.hnyfkj.jyindustry.biz.item.entity.BarItem;
import com.hnyfkj.jyindustry.biz.kehu.dao.UserDao;
import com.hnyfkj.jyindustry.biz.kehu.entity.User;
import com.hnyfkj.jyindustry.biz.xiaofei.dao.XiaofeiLogDao;
import com.hnyfkj.jyindustry.biz.xiaofei.entity.XiaofeiLog;
import com.hnyfkj.jyindustry.common.dto.PageReturnDto;
import com.hnyfkj.jyindustry.common.dto.RespDto;
import com.hnyfkj.jyindustry.common.utils.ShiroUtils;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;

import java.util.List;

@Service("chongzhiLogService")
public class ChongzhiLogServiceImpl extends ServiceImpl<ChongzhiLogDao, ChongzhiLog> implements ChongzhiLogService {


    @Autowired
    private ChongzhiLogDao chongzhiLogDao;


    @Autowired
    private UserDao userDao;

    @Autowired
    private BarItemDao barItemDao;

    @Autowired
    private XiaofeiLogDao xiaofeiLogDao;

    @Override
    public PageReturnDto<ChongzhiLog> queryPage(ChongzhiLogQueryDto queryDto) {
        User user = userDao.selectById(queryDto.getId());
        queryDto.setPhone(user.getPhone());
        PageInfo<ChongzhiLog> page =
                PageHelper.startPage(queryDto.getPage(), queryDto.getLimit()).doSelectPageInfo(() -> chongzhiLogDao.selectListByQuery(queryDto));
        return PageReturnDto.pageOf(page);
    }

    @Override
    public RespDto<String> insert(ChongzhiLogInsertDto chongzhiLogInsertDto) {
        //充值消费完后
        User user = userDao.selectById(chongzhiLogInsertDto.getId());

        if (user.getChongzhi() < chongzhiLogInsertDto.getXiaoFeiMoney()) {
            return RespDto.succ("账户余额不足!请充值后消费!");
        }
        user.setJianfacount(user.getJianfacount() + chongzhiLogInsertDto.getChongJianFa());
        user.setGanxicount(user.getGanxicount() + chongzhiLogInsertDto.getChongGanXi());
        user.setXifacount(user.getXifacount() + chongzhiLogInsertDto.getChongXiFa());
        user.setChongzhi(user.getChongzhi() + chongzhiLogInsertDto.getChongZhiMoney() - chongzhiLogInsertDto.getXiaoFeiMoney());
        userDao.updateById(user);

        //存储记录表
        //充值次数记录
        if (chongzhiLogInsertDto.getChongJianFa() != 0) {
            ChongzhiLog chongzhiLog = new ChongzhiLog();
            chongzhiLog.setPhone(user.getPhone());
            chongzhiLog.setName(user.getMemberName());
            chongzhiLog.setChongzhi("剪发充值:" + chongzhiLogInsertDto.getChongJianFa() + "次");
            chongzhiLogDao.insert(chongzhiLog);
        }
        //充值次数记录
        if (chongzhiLogInsertDto.getChongGanXi() != 0) {
            ChongzhiLog chongzhiLog = new ChongzhiLog();
            chongzhiLog.setPhone(user.getPhone());
            chongzhiLog.setName(user.getMemberName());
            chongzhiLog.setChongzhi("干洗充值:" + chongzhiLogInsertDto.getChongGanXi() + "次");
            chongzhiLogDao.insert(chongzhiLog);
        }

        //充值次数记录
        if (chongzhiLogInsertDto.getChongXiFa() != 0) {
            ChongzhiLog chongzhiLog = new ChongzhiLog();
            chongzhiLog.setPhone(user.getPhone());
            chongzhiLog.setName(user.getMemberName());
            chongzhiLog.setChongzhi("干洗充值:" + chongzhiLogInsertDto.getChongXiFa() + "次");
            chongzhiLogDao.insert(chongzhiLog);
        }
        //充值金额记录
        if (chongzhiLogInsertDto.getChongZhiMoney() != 0) {
            ChongzhiLog chongzhiLog = new ChongzhiLog();
            chongzhiLog.setPhone(user.getPhone());
            chongzhiLog.setName(user.getMemberName());
            chongzhiLog.setChongzhi(chongzhiLogInsertDto.getChongZhiMoney() + "元");
            chongzhiLogDao.insert(chongzhiLog);
            return RespDto.succ("充值成功!");
        }
        //消费金额记录
        if (chongzhiLogInsertDto.getXiaoFeiMoney() != 0) {
            String memberId = ShiroUtils.getSessionAttribute("memberId").toString();
            List<BarItem> shop_id = barItemDao.selectList(new QueryWrapper<BarItem>().eq("shop_id", memberId));
            //存储消费项目
            for (BarItem barItem : shop_id) {
                if ((barItem.getId()+"").equals(chongzhiLogInsertDto.getSellState())) {
                    chongzhiLogInsertDto.setType(barItem.getConsumptionItems());
                }
            }
            XiaofeiLog xiaofeiLog = new XiaofeiLog();
            xiaofeiLog.setPhone(user.getPhone());
            xiaofeiLog.setName(user.getMemberName());
            xiaofeiLog.setXiaofei(chongzhiLogInsertDto.getType()+chongzhiLogInsertDto.getXiaoFeiMoney() + "元");
            xiaofeiLogDao.insert(xiaofeiLog);
            return RespDto.succ("消费成功!");
        }
        return RespDto.succ("您未操作!");

    }

    @Override
    public int update(ChongzhiLogUpdateDto chongzhiLogUpdateDto) {
        final ChongzhiLog chongzhiLog = new ChongzhiLog();
        BeanUtils.copyProperties(chongzhiLogUpdateDto, chongzhiLog);
        return chongzhiLogDao.updateById(chongzhiLog);
    }

}